package Pages;

import org.hamcrest.core.StringContains;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static Driver.DriverSingleton.*;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;

public class RecordListPage extends BasePage{

    WebDriver driver = getChromeDriver();
    Map<String,Object> vars = getVars();

    /*
    1.日期姓名查询
     */
    public  Calendar StrToCalendar_hms(String str) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        Date date = new Date();
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public  Calendar StrToCalendar_ymd1(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        Date date = new Date();
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    public  Calendar StrToCalendar_ymd2(String str) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date();
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }


    public  String CalendarToStr_hms(Calendar calendar) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        String str = format.format(calendar.getTime());
        return str;
    }

    public  String CalendarToStr_ymd(Calendar calendar) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String str = format.format(calendar.getTime());
        return str;
    }

    public String belongCalendar(Calendar nowDate, Calendar beginDate,Calendar endDate) {
        if (nowDate.after(beginDate) && nowDate.before(endDate)) {
            return "true";
        } else {
            return "false";
        }
    }

    public RecordListPage goRecordListPage(){
        super.login();
        driver.findElement(By.cssSelector(".el-menu-item:nth-child(2)")).click();
        waitForWindow(2000);
        assertThat(driver.getTitle(), is("咨询记录列表"));
        return this;
    }

    //返回今日咨询总时长
    public String getTodayCstTimeFromRecord(){

        waitForWindow(2000);
        //选择时间
        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        //选择今日时间
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).click();

        waitForWindow(2000);
        //键盘全选
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys(Keys.CONTROL,"a");
        //键盘删除
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys(Keys.BACK_SPACE);
        //键盘键入
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys("00:00:00");
        //点击确定
        driver.findElement(By.cssSelector(".is-plain > span")).click();

        waitForWindow(2000);
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr/td[4]"));

        List <Calendar> times= new ArrayList<Calendar>();
        for(WebElement element: elements){
            times.add(StrToCalendar_hms(element.getText()));
        }
        Calendar cTotal = StrToCalendar_hms("00:00:00");
        for(Calendar time : times){
            cTotal.add(Calendar.HOUR_OF_DAY, time.get(Calendar.HOUR_OF_DAY));

            cTotal.add(Calendar.MINUTE, time.get(Calendar.MINUTE));

            cTotal.add(Calendar.SECOND, time.get(Calendar.SECOND));
        }
        String total_time = CalendarToStr_hms(cTotal);

        return total_time;
    }

    //返回累计咨询数量
    public String getAllCstFromRecord(){

        waitForWindow(2000);
        List<WebElement> elements1 = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr"));
        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[3]/div[2]/button[2]")).click();
        waitForWindow(2000);
        List<WebElement> elements2  = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr"));
        String cons = String.valueOf(elements1.size()+elements2.size());
        return cons;

    }

    //返回今日咨询数量
    public String getTodayCstFromRecord(){

        waitForWindow(2000);
        //选择时间
        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        //选择今日时间
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).click();
        waitForWindow(2000);
        //键盘全选
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys(Keys.CONTROL,"a");
        //键盘删除
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys(Keys.BACK_SPACE);
        //键盘键入
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys("00:00:00");
        //点击确定
        driver.findElement(By.cssSelector(".is-plain > span")).click();

        waitForWindow(2000);
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr"));
        String cons = String.valueOf( elements.size());
        return cons;

    }

    //输入访客姓名
    public void inputVisitorName(String name){
        driver.findElement(By.cssSelector(".nameInput > .el-input__inner")).click();
        driver.findElement(By.cssSelector(".nameInput > .el-input__inner")).sendKeys(name);
        driver.findElement(By.cssSelector(".nameInput > .el-input__inner")).sendKeys(Keys.ENTER);
    }

    //输入时间范围
    public void inputDate(String startDate, String startTime, String endDate, String endTime){
        driver.findElement(By.cssSelector(".el-range-input:nth-child(2)")).click();
        waitForWindow(2000);
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(1) .el-input__inner")).sendKeys(startDate);
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1)")).click();
        driver.findElement(By.cssSelector(".el-date-range-picker__editors-wrap:nth-child(1) > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys(startTime);
        driver.findElement(By.cssSelector(".is-right > .el-date-range-picker__time-picker-wrap:nth-child(1) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".is-right > .el-date-range-picker__time-picker-wrap:nth-child(1) .el-input__inner")).sendKeys(endDate);
        driver.findElement(By.cssSelector(".is-right > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).click();
        driver.findElement(By.cssSelector(".is-right > .el-date-range-picker__time-picker-wrap:nth-child(2) .el-input__inner")).sendKeys(endTime);
        driver.findElement(By.cssSelector(".is-plain > span")).click();
    }

    //获取表格中访客姓名的列表
    public List<WebElement> getVisitor(){
        waitForWindow(2000);
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr/td[3]"));
        return elements;
    }

    //获取表格中时间的列表
    public List<WebElement> getDate(){
        waitForWindow(2000);
        List<WebElement> elements = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr/td[5]"));
        return elements;
    }

    //根据访客姓名搜索咨询记录
    public void searchRecordByVisitor(String name) {
        this.inputVisitorName(name);
        List<WebElement> elements1 = getVisitor();
        for(WebElement element: elements1){
            assertThat( element.getText(), containsString(name) );
        }
        //翻页
        driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[3]/div[2]/button[2]")).click();
        waitForWindow(2000);
        List<WebElement> elements2 = getVisitor();
        for(WebElement element: elements2){
            assertThat( element.getText(), containsString(name) );
        }
    }

    //根据输入日期搜索咨询记录
    public void searchRecordByDate(String startDate, String startTime, String endDate, String endTime) {
        this.inputDate(startDate,startTime,endDate,endTime);

        if( startDate.compareTo(endDate) < 0  || (startDate.equals(endDate) && startTime.compareTo(endDate) < 0) ){
            WebElement element1 = driver.findElement(By.cssSelector(".el-range-input:nth-child(2)"));
            WebElement element2 = driver.findElement(By.cssSelector(".el-range-input:nth-child(4)"));
            assertThat(element1.getText(), is(element2.getText()));
        }else{
            waitForWindow(2000);
            List<WebElement> elements = getDate();
            for(WebElement element: elements){
                Calendar now = Calendar.getInstance();
                now = StrToCalendar_ymd1(element.getText());
                Calendar begin = Calendar.getInstance();
                begin = StrToCalendar_ymd2(startDate+" "+startTime.substring(0,5));
                Calendar end = Calendar.getInstance();
                end = StrToCalendar_ymd2(endDate+" "+endTime.substring(0,5));
                String belongDate = belongCalendar(now,begin,end);
                assertThat(belongDate,is("true"));
            }
        }

    }

    //根据访客姓名和输入日期搜索咨询记录
    public void searchRecordByNameAndDate(String name, String startDate, String startTime, String endDate, String endTime) {
        //输入访客姓名
        inputVisitorName(name);
        //输入日期时间
        inputDate(startDate,startTime,endDate,endTime);

        if( startDate.compareTo(endDate) < 0  || (startDate.equals(endDate) && startTime.compareTo(endDate) < 0) ){
            WebElement element1 = driver.findElement(By.cssSelector(".el-range-input:nth-child(2)"));
            WebElement element2 = driver.findElement(By.cssSelector(".el-range-input:nth-child(4)"));
            assertThat(element1.getText(), is(element2.getText()));
        }else{
            waitForWindow(2000);

            List<WebElement> elements1 = getVisitor();
            for(WebElement element: elements1){
                assertThat( element.getText(), containsString(name) );
            }

            List<WebElement> elements2 = getDate();
            for(WebElement element: elements2){
                Calendar now = Calendar.getInstance();
                now = StrToCalendar_ymd1(element.getText());
                Calendar begin = Calendar.getInstance();
                begin = StrToCalendar_ymd2(startDate+" "+startTime.substring(0,5));
                Calendar end = Calendar.getInstance();
                end = StrToCalendar_ymd2(endDate+" "+endTime.substring(0,5));
                String belongDate = belongCalendar(now,begin,end);
                assertThat(belongDate,is("true"));
            }
        }

    }

    /*
    2.星级评价
     */

    //在list中获取星级评价值
    public int getStarFromRecordList(){
        List<WebElement> elements1 = driver.findElements(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr[1]/td[6]/div/div"));
        int count = 0;
        for(WebElement element : elements1){
            if(element.getAttribute("style").equals("color: rgb(247, 186, 42);") ){
                count++;
            }
        }
        System.out.println(count);
        return count;
    }

    //在list中获取时间
    public String getTimeFromRecordList(){
        waitForWindow(2000);
        String time = driver.findElement(By.xpath("//*[@id=\"app\"]/div/div[2]/div[2]/div/div[2]/div[3]/table/tbody/tr[1]/td[4]/div/div")).getText();
        return time;
    }


    /*
    咨询记录导出
     */

    //导出某一条咨询记录
    public void downloadRecord() {
        waitForWindow(2000);
        driver.findElement(By.xpath("//button[contains(.,'导出记录')]")).click();
        String info = getDialog();
        assertThat( info, containsString("确认导出") );

        driver.findElement(By.cssSelector(".el-button--small:nth-child(2) > span")).click();
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-button:nth-child(1) > span")).click();

        waitForWindow(2000);
        System.out.println(driver.findElement(By.cssSelector(".content:nth-child(1)")).getText());

    }

    //选中前两条聊天记录
    public RecordListPage select2records(){
        driver.findElement(By.cssSelector(".el-table__row:nth-child(1) .el-checkbox__inner")).click();
        driver.findElement(By.cssSelector(".el-table__row:nth-child(2) .el-checkbox__inner")).click();
        return this;
    }

    //全选聊天记录
    public RecordListPage selectAllRecords(){
        driver.findElement(By.cssSelector(".is-leaf .el-checkbox__inner")).click();
        return this;
    }

    //点击批量导出聊天记录按钮
    public RecordListPage clickExportButton(){
        driver.findElement(By.xpath("//button[contains(.,'批量导出咨询记录')]")).click();
        return this;
    }

    //确认
    public RecordListPage confirm(){
        driver.findElement(By.xpath("//button[contains(.,'确认')]")).click();
        return this;
    }

    //取消
    public RecordListPage cancel(){
        driver.findElement(By.xpath("//button[contains(.,'取消')]")).click();
        return this;
    }



}
